const USERINFO_KEY = "USERINFO_KEY"
const SEARCHLIST_KEY = "SEARCHLIST_KEY"

const defaultUserinfo = {token:'', userId:''}

// 设置storage中的userinfo数据
export const setLocalInfo = function(userinfo){

    const str = JSON.stringify(userinfo)
    localStorage.setItem(USERINFO_KEY, str)
}

// 获取用户信息
export const getLocalInfo = function(){
    const local = localStorage.getItem(USERINFO_KEY) 
    const userInfo = local != undefined ? JSON.parse(local) : defaultUserinfo
    return userInfo
}

// 删除用户信息
export const deleteLocalInfo = function(){
    localStorage.removeItem(USERINFO_KEY)
}

// 存储搜索历史
export const setSearchList = (value)=>{ 
    localStorage.setItem(SEARCHLIST_KEY, JSON.stringify(value))
}

// 获取搜索历史
export const getSearchList = ()=>{
    const local = localStorage.getItem(SEARCHLIST_KEY)
    const list = local ? JSON.parse(local) : []
    return list
}

// 删除搜索历史
export const deleteSearchList = ()=>{
    localStorage.removeItem(SEARCHLIST_KEY)
}